#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/6/13 9:11
# @Author  : khz_df
# @Site    : ${SITE}
# @File    : PhpApi.py
# @Software: ce9nt


import os

import requests
from Crypto.Cipher import ARC4


class PhpApi(object):
    def __init__(self):
        self.key = b'uvT%3(Bd&394kaas'
        # self.host = 'http://local.zhw.com/'
        # self.host = 'http://192.168.3.55/'
        # self.host = 'http://www1.zuhaowan.com:97/'
        self.host = 'http://www.zuhaowan.com/'

    def _enc(self, info):
        data = bytearray(info, 'utf-8')
        enc_data = ARC4.new(self.key).encrypt(data)
        return enc_data.hex()

    def _dec(self, info):
        data = bytes.fromhex(info)
        dec_data = ARC4.new(self.key).decrypt(data)
        return dec_data.decode('utf-8')

    def req_account(self, max_id):
        """
        task/hao/sendHaoAccount?data=xxxxxxxxxxx
        :return:
        """
        data = 'max_id=%d' % max_id
        url = self.host + 'task/security/sendHaoAccount?token=a2sf3aw4eb5aw6erew&data=' + self._enc(data)
        print('[req_account] url = %s' % url)

        enc_accounts = requests.request(url=url, method='GET')
        accounts = self._dec(enc_accounts.text)
        print(accounts)

        return accounts

    def receiveHaoAccount(self, shelf_id, gid, account, msg, rapid_info):
        data = """param=[{"id":"%s", "gid":"%s", "account":"%s", "msg":"%s", "rapid_info":"%s"}]""" % (
            shelf_id, gid, account, msg, rapid_info)
        print(data)
        return
        url = self.host + 'task/security/receiveHaoAccount?token=a2sf3aw4eb5aw6erew&data=' + self._enc(data)
        print(url)
        enc_result = requests.request(url=url, method='GET')
        result = self._dec(enc_result.text)
        print(result)


def main():
    # PhpApi().receiveHaoAccount('191245', 'lvzicheng159', '密码错误', '')
    # PhpApi().receiveHaoAccount('219904', 'hhhhffffhf', '密码错误', '')
    PhpApi().receiveHaoAccount('3858777', '272', '1476252772', '密码错误', '')
    PhpApi().receiveHaoAccount('3860197', '272', '3382699295', '用户不存在', '')
    return


if __name__ == "__main__":
    print("------------------    Enter __main__    ------------------")

    print(u"[Current work directory is : ]\t" + os.getcwd())
    print(u"[Current process ID is : ]\t" + str(os.getpid()))
    print("\n")
    main()

    print("------------------    Leave __main__    ------------------")
